Kotlin provides the operators as
and as?
to cast an expression to a specific type, and is
to check the
assignment compatibility with a type. These operators are used for downcasts, smart casts, and run-time type checking.
In case the as
or as?
operator is used for upcasting from a subtype to a supertype, the cast is redundant as it has no
effect and can never fail. If a specific type is expected, an expression of a subtype can always be inserted without casting (Substitution Principle
and Assignment Compatibility).
Likewise, the is
operator is redundant and will always return true
if the type of the expression on the left side is
assignment compatible with the type on the right.
What is the potential impact?
Code redundancy
Since the operation will always succeed and has no side effects, it is pointless to use it. Conditions with is
will lead to dead code
branches because they will always or never be satisfied.